package com.nitty.cash.loop

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Typeface
import com.nitty.cash.loop.base.BaseApplication
import com.nitty.cash.loop.event.NittyEventPublisher
import com.nitty.cash.loop.request.HttpHelper
import com.nitty.cash.loop.request.HttpLoader
import com.nitty.cash.loop.utils.NittyDateUtil
import com.nitty.cash.loop.utils.helper.NittyAnalysisHelper
import com.nitty.cash.loop.utils.log.NittyLogger
import com.nitty.cash.loop.utils.shared.NittySharedHelper
import com.nitty.cash.loop.utils.shared.NittySharedKey
import java.lang.reflect.Type

/**
 * App
 * @Author wwdeng
 * @CreateTime: 2024/11/18 13:35
 * @Annotation：
 */
class NittyApp: BaseApplication() {
    //事件订阅
    lateinit var publisher: NittyEventPublisher
    private lateinit var typefaceBlack: Typeface
    private lateinit var typefaceBold: Typeface
    private lateinit var typefaceMedium: Typeface

    companion object {
        @SuppressLint("StaticFieldLeak")
        lateinit var app: NittyApp
    }

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        app = this
        publisher = NittyEventPublisher()
    }

    override fun onCreate() {
        super.onCreate()
        typefaceBlack = Typeface.createFromAsset(assets,"fonts/Roboto-Black.ttf")
        typefaceBold = Typeface.createFromAsset(assets,"fonts/Roboto-Bold.ttf")
        typefaceMedium = Typeface.createFromAsset(assets,"fonts/Roboto-Medium.ttf")

        //保存APP启动时间
        val startTime = NittyDateUtil.getFormatTime(System.currentTimeMillis(),"dd/MM/yyyy HH:mm:ss")
        NittySharedHelper.putNittySharedData(NittySharedKey.NITTY_APP_START_TIME,startTime)

        NittySharedHelper.putNittySharedData(NittySharedKey.NITTY_SECRET_KEY,"yx3IEPjUDhfMBzHJ")
        NittySharedHelper.putNittySharedData(NittySharedKey.NITTY_SECRET_IV,"vgUYGxXwr2Rxw3xC")
        initLibConfigs()
    }

    /**
     * 初始化Lib
     */
    private fun initLibConfigs(){
        NittyLogger.debug(true)
        HttpHelper.init(HttpLoader())
        //初始化 analysis
        NittyAnalysisHelper.initAnalysisLibAtAppLaunch()
    }

    fun getBlackTypeface(): Typeface{
        return typefaceBlack
    }

    fun getBoldTypeface(): Typeface{
        return typefaceBold
    }

    fun getMediumTypeface(): Typeface{
        return typefaceMedium
    }

    /**
     * 获取当前手机屏幕的宽
     */
    fun getPhoneWidth(): Int {
        // 手机屏幕的宽
        // 获取DisplayMetrics对象
        val displayMetrics = resources.displayMetrics
        return displayMetrics.widthPixels
    }

    /**
     * 获取当前手机屏幕的高
     */
    fun getPhoneHeight(): Int {
        // 手机屏幕的宽
        // 获取DisplayMetrics对象
        val displayMetrics = resources.displayMetrics
        return displayMetrics.heightPixels
    }
}